Imports System.Xml
Imports System.IO

Public Class MapperUtils

    Public Shared Function FindImportType(ByVal cols() As Core.MappedColumn) As ImportSelection
        Dim result As ImportSelection = ImportSelection.None

        For i As Integer = 0 To cols.Length - 1
            Select Case cols(i).ColumnName
                Case "parentid"
                    ' Category or Product
                    result = ImportSelection.Products
                    For j As Integer = 0 To cols.Length - 1
                        If cols(j).ColumnName = "sourcetype" Then
                            result = ImportSelection.Categories
                            Exit For
                        End If
                    Next
                    Exit For
                Case "username"
                    result = ImportSelection.Users
                    Exit For
                Case "producttypename"
                    result = ImportSelection.ProductTypes
                    Exit For
            End Select
        Next

        Return result
    End Function
    Public Shared Function ParseColumns(ByVal l As String) As Core.MappedColumn()
        Dim result As Core.MappedColumn()

        Dim columns() As String = l.Split(vbTab)
        ReDim result(columns.Length - 1)

        For i As Integer = 0 To columns.Length - 1
            result(i) = New Core.MappedColumn(i, ParseColumnName(columns(i)))
        Next

        Return result
    End Function

    Public Shared Function ParseColumnName(ByVal input As String) As String
        Dim result As String = input
        result = result.Trim.ToLower
        Return result
    End Function

    Public Shared Function ParseFieldString(ByVal fieldData As String, ByVal originalData As String) As String
        Dim result As String = originalData

        If fieldData.Trim.ToLower = "empty" Then
            result = String.Empty
        Else
            If fieldData.Trim <> String.Empty Then
                result = fieldData
            End If
        End If

        Return result
    End Function

    Public Shared Function ParseFieldBoolean(ByVal fieldData As String, ByVal originalData As Boolean) As Boolean
        Dim result As Boolean = originalData

        Dim s As String = fieldData.Trim.ToLower

        Select Case s
            Case "f", "false", "0", "flase"
                result = False
            Case "t", "true", "1", "ture", "treu"
                result = True
        End Select

        Return result
    End Function

    Public Shared Function ParseFieldDecimal(ByVal fieldData As String, ByVal originalData As Decimal) As Decimal
        Dim result As Decimal = originalData

        If fieldData.Trim.ToLower = "empty" Then
            result = 0D
        Else
            If fieldData.Trim <> String.Empty Then
                Dim temp As Decimal = 0
                If Decimal.TryParse(fieldData, temp) Then
                    result = temp
                End If
            End If
        End If

        Return result
    End Function

    Public Shared Function ParseFieldInteger(ByVal fieldData As String, ByVal originalData As Integer) As Integer
        Dim result As Integer = originalData

        If fieldData.Trim.ToLower = "empty" Then
            result = 1
        Else
            If fieldData.Trim <> String.Empty Then
                Dim temp As Integer = 1
                If Integer.TryParse(fieldData, temp) Then
                    result = temp
                End If
            End If
        End If

        Return result
    End Function

    Public Shared Function TrimQuotes(ByVal input As String) As String
        Dim result As String = input.TrimStart("""")
        result = result.TrimEnd("""")
        Return result
    End Function

    '#Region " Address Functions "

    '    Public Shared Function ParseAddressBookXml(ByVal x As String) As BVweb.Address()
    '        Dim list As New ArrayList

    '        Try
    '            list.Clear()

    '            If x.Trim.Length > 0 Then
    '                Dim xdoc As New XmlDocument
    '                xdoc.LoadXml(x)

    '                Dim addressNodes As XmlNodeList
    '                addressNodes = xdoc.SelectNodes("/AddressBook/Address")

    '                If Not addressNodes Is Nothing Then
    '                    For i As Integer = 0 To addressNodes.Count - 1
    '                        Dim a As New BVweb.Address
    '                        a = ParseSingleAddress(addressNodes(i).OuterXml)
    '                        list.Add(a)                        
    '                    Next
    '                End If
    '            End If

    '        Catch ex As Exception

    '        End Try

    '        Dim result(list.Count - 1) As BVweb.Address
    '        For i As Integer = 0 To list.Count - 1
    '            result(i) = CType(list(i), BVweb.Address)
    '        Next

    '        If result.Length < 1 Then
    '            ReDim result(0)
    '            result(0) = New BVweb.Address
    '        End If
    '        Return result

    '    End Function

    '    Public Shared Function ParseSingleAddress(ByVal x As String) As BVweb.Address
    '        Dim result As New BVweb.Address

    '        Try
    '            If x.Trim.Length > 0 Then
    '                Dim xdoc As New XmlDocument
    '                xdoc.LoadXml(x)
    '                If Not xdoc.SelectSingleNode("/Address") Is Nothing Then
    '                    result.ID = XmlHelper.XPathToInteger(xdoc, "/Address/ID")
    '                    result.FirstName = XmlHelper.XPathToString(xdoc, "/Address/FirstName")
    '                    result.MiddleInitial = XmlHelper.XPathToString(xdoc, "/Address/MiddleInitial")
    '                    result.LastName = XmlHelper.XPathToString(xdoc, "/Address/LastName")
    '                    result.CompanyName = XmlHelper.XPathToString(xdoc, "/Address/CompanyName")
    '                    result.StreetLine1 = XmlHelper.XPathToString(xdoc, "/Address/StreeLine1")
    '                    result.StreetLine2 = XmlHelper.XPathToString(xdoc, "/Address/StreetLine2")
    '                    result.City = XmlHelper.XPathToString(xdoc, "/Address/City")
    '                    result.StateName = XmlHelper.XPathToString(xdoc, "/Address/StateName")
    '                    result.StateCode = XmlHelper.XPathToString(xdoc, "/Address/StateCode")
    '                    result.PostalCode = XmlHelper.XPathToString(xdoc, "/Address/PostalCode")
    '                    result.CountryName = XmlHelper.XPathToString(xdoc, "/Address/CountryName")
    '                    result.CountryCode = XmlHelper.XPathToString(xdoc, "/Address/CountryCode")
    '                    result.PhoneNumber = XmlHelper.XPathToString(xdoc, "/Address/PhoneNumber")
    '                    result.FaxNumber = XmlHelper.XPathToString(xdoc, "/Address/FaxNumber")
    '                    result.WebSiteURL = XmlHelper.XPathToString(xdoc, "/Address/WebSiteUrl")
    '                End If
    '            End If

    '        Catch ex As Exception
    '        End Try

    '        Return result
    '    End Function

    '    Public Shared Function WriteAddressBookToXml(ByVal addresses() As BVweb.Address) As String
    '        Dim results As String = String.Empty

    '        Try
    '            Dim sw As StringWriter = New StringWriter
    '            Dim xw As XmlTextWriter = New XmlTextWriter(sw)

    '            xw.Formatting = Formatting.Indented
    '            xw.Indentation = 3
    '            xw.WriteStartDocument()

    '            WriteAddressBookToXmlWriter(xw, addresses)

    '            xw.WriteEndDocument()
    '            xw.Flush()
    '            xw.Close()

    '            results = sw.GetStringBuilder.ToString()
    '            sw.Close()

    '        Catch ex As Exception

    '        End Try
    '        Return results
    '    End Function

    '    Public Shared Sub WriteAddressBookToXmlWriter(ByRef xw As XmlTextWriter, ByVal addresses() As BVweb.Address)
    '        '--------------------------------------------            
    '        ' Start Address
    '        xw.WriteStartElement("AddressBook")

    '        For Each a As BVweb.Address In addresses
    '            WriteAddressToXmlWriter(xw, a)
    '        Next

    '        xw.WriteEndElement()
    '        ' End AddressBook
    '        '--------------------------------------------
    '    End Sub

    '    Public Shared Function WriteAddressToXml(ByVal a As BVweb.Address) As String
    '        Dim response As String = String.Empty
    '        Try
    '            Dim sw As StringWriter = New StringWriter
    '            Dim xw As XmlTextWriter = New XmlTextWriter(sw)

    '            xw.Formatting = Formatting.Indented
    '            xw.Indentation = 3
    '            xw.WriteStartDocument()

    '            WriteAddressToXmlWriter(xw, a)

    '            xw.WriteEndDocument()
    '            xw.Flush()
    '            xw.Close()

    '            response = sw.GetStringBuilder.ToString()
    '            sw.Close()

    '        Catch ex As Exception

    '        End Try

    '        Return response
    '    End Function

    '    Public Shared Sub WriteAddressToXmlWriter(ByRef xw As System.Xml.XmlTextWriter, ByVal a As BVweb.Address)
    '        '--------------------------------------------            
    '        ' Start Address
    '        xw.WriteStartElement("Address")

    '        xw.WriteElementString("ID", a.ID)
    '        xw.WriteElementString("FirstName", a.FirstName)
    '        If a.MiddleInitial.Length > 0 Then
    '            xw.WriteElementString("MiddleInitial", a.MiddleInitial)
    '        End If
    '        xw.WriteElementString("LastName", a.LastName)
    '        If a.CompanyName.Length > 0 Then
    '            xw.WriteElementString("CompanyName", a.CompanyName)
    '        End If
    '        xw.WriteElementString("StreeLine1", a.StreetLine1)
    '        If a.StreetLine2.Length > 0 Then
    '            xw.WriteElementString("StreetLine2", a.StreetLine2)
    '        End If
    '        xw.WriteElementString("City", a.City)
    '        If a.StateName.Length > 0 Then
    '            xw.WriteElementString("StateName", a.StateName)
    '        End If
    '        If a.StateCode.Length > 0 Then
    '            xw.WriteElementString("StateCode", a.StateCode)
    '        End If
    '        If a.PostalCode.Length > 0 Then
    '            xw.WriteElementString("PostalCode", a.PostalCode)
    '        End If
    '        If a.CountryName.Length > 0 Then
    '            xw.WriteElementString("CountryName", a.CountryName)
    '        End If
    '        If a.CountryCode.Length > 0 Then
    '            xw.WriteElementString("CountryCode", a.CountryCode)
    '        End If
    '        If a.PhoneNumber.Length > 0 Then
    '            xw.WriteElementString("PhoneNumber", a.PhoneNumber)
    '        End If
    '        If a.FaxNumber.Length > 0 Then
    '            xw.WriteElementString("FaxNumber", a.FaxNumber)
    '        End If
    '        If a.WebSiteURL.Length > 0 Then
    '            xw.WriteElementString("WebSiteUrl", a.WebSiteURL)
    '        End If

    '        xw.WriteEndElement()
    '        ' End Address
    '        '--------------------------------------------
    '    End Sub

    '#End Region

End Class
